package com.dotlrn.mobile.nfc;

import com.dotlrn.mobile.R;


import android.app.Activity;
import android.app.AlertDialog;
import android.app.PendingIntent;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.nfc.NfcAdapter;
import android.nfc.Tag;
import android.os.Bundle;
import android.widget.TextView;

public class DotLRNNFCContextualLearningTagIDActivity extends Activity {

	static final int ACTIVITY_TIMEOUT_MS = 1 * 1000;
	private TextView tagIdTextView;

	private NfcAdapter mAdapter;
	private byte[] tagId;
	private static final byte[] HEX_CHAR_TABLE = { (byte) '0', (byte) '1',
			(byte) '2', (byte) '3', (byte) '4', (byte) '5', (byte) '6',
			(byte) '7', (byte) '8', (byte) '9', (byte) 'A', (byte) 'B',
			(byte) 'C', (byte) 'D', (byte) 'E', (byte) 'F' };


	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.read_tag_id);
		tagIdTextView = (TextView) findViewById(R.id.tag_id);
		
		mAdapter = NfcAdapter.getDefaultAdapter(this);
		
		if(!mAdapter.isEnabled()){
		
			AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setMessage("Please enable you NFC Adaptar")
			       .setCancelable(false)
			       .setPositiveButton("back", new DialogInterface.OnClickListener() {
			           public void onClick(DialogInterface dialog, int id) {
			                finish();
			           }
			       });
			AlertDialog alert = builder.create();
			alert.show();
		}
	}	
	
	void resolveIntent(Intent intent) {
		
		MediaPlayer mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.blip);
		mediaPlayer.setOnCompletionListener(new OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mediaPlayer) {
            	mediaPlayer.release();
            }
        });

		mediaPlayer.start();

		Tag myTag = (Tag) intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
		tagId = myTag.getId();
		tagIdTextView.setText("Tag id: " + getHexString(tagId, tagId.length));	
	}
	
	@Override
	public void onResume() {

		super.onResume();
		IntentFilter discovery = new IntentFilter(
				NfcAdapter.ACTION_TAG_DISCOVERED);
		IntentFilter[] tagFilters = new IntentFilter[] { discovery };
		Intent i = new Intent(this, getClass())
				.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
		PendingIntent pi = PendingIntent.getActivity(this, 0, i, 0);
		mAdapter.enableForegroundDispatch(this, pi, tagFilters, null);
	}

	@Override
	public void onPause() {
		super.onPause();
		mAdapter.disableForegroundDispatch(this);
	}

	@Override
	public void onNewIntent(Intent intent) {
		resolveIntent(intent);
	}

	public static String getHexString(byte[] raw, int len) {
		byte[] hex = new byte[2 * len];
		int index = 0;
		int pos = 0;
		for (byte b : raw) {
			if (pos >= len)
				break;
			pos++;
			int v = b & 0xFF;
			hex[index++] = HEX_CHAR_TABLE[v >>> 4];
			hex[index++] = HEX_CHAR_TABLE[v & 0xF];
		}
		return new String(hex);
	}
		
}
